<div style="padding:8px 8px 0 4px;font-weight: bold;"><i class="am-icon-home am-icon-cloud-download" style="margin-right:12px;"></i>升级包下载</div>
<!-- 查询区 -->
<div id="searchContainer"></div>
<div class="row1 up_row">
    <div class="data_container">
        <!-- 按钮区 -->
        <div id="buttonsDiv">
        </div>
        <!-- 数据列表区 -->
        <div id="contentDiv" class="content_div"></div>
    </div>
</div>
<script src="/public/js/timeago/timeago.min.js"></script>
<script>
$(function() {
    // 加载查询区
    var searchBar = new SearchBar({
        containerId: "searchContainer",
        textPlaceholder: '查询条件：模块名、升级包名、升级内容',
        queryList: [
            { title: "升级项目", key: "packageProjectId", url: "/select/getUpgradeProjects?key=packageProjectId", forSort: 10 },
            { title: "升级状态", key: "is_finish_deploy", data: { key: "is_finish_deploy", datas: [{ code: "1", name: "已完成升级" }, { code: "0", name: "未完成升级" }] } },
            { title: "验证状态", key: "is_verification", data: { key: "is_verification", datas: [{ code: "1", name: "已完成验证" }, { code: "0", name: "未完成验证" }] } }
        ],
        queryFunc: function() { load(); }
    });
    searchBar.render();

    var firstQuery = true;

    // 加载表格
    var workGrid = new WorkGrid({
        url: '/simpleDownUpgradePackage/query',
        searchBar: searchBar,
        columns: [
            { display: "所属项目", property: "project_name", sort: "project_name", width: "8%" },
            { display: "模块", property: "module", sort: "module", width: "7%" },
            {
                display: "描述",
                property: "description",
                sort: "description",
                view: function(col) {
                    return '(' + col.name + ') ' + noHighlight(col.description) + '&nbsp;<span style="color:#EF971C;font-weight:bold;">' + (col.special_remark || '') + '</span>';
                }
            },
            {
                display: "备注",
                width: "9%",
                view: function(col) {
                    var html = '';
                    html += (html !== '' ? ' ' : '') + (col.is_need_reboot === 1 ? '<span style="color:#ec5656">重启</span>' : '');
                    html += (html !== '' ? ' ' : '') + (col.is_need_clearcache === 1 ? '<span style="color:#ec5656">清除缓存</span>' : '');
                    html += (html !== '' ? ' ' : '') + (col.is_need_delfile === 1 ? '<span style="color:#ec5656">删除classes目录</span>' : '');
                    html += (html !== '' ? ' ' : '') + (col.is_need_exescript === 1 ? '<span style="color:#ec5656">手动执行脚本</span>' : '');
                    return html;
                }
            },
            { display: "发布人", property: "publish_username", sort: "publish_username", width: "5%" },
            {
                display: "发布时间",
                sort: "publish_time",
                width: "6%",
                view: '<div class="need_to_be_rendered" role="tip" tip="{{publish_time_display}}" datetime="{{publish_time_display}}"></div>'
            },
            {
                display: "是否部署",
                sort: "is_finish_deploy",
                width: "6%",
                view: function(col) {
                    if (col.is_finish_deploy === 1) {
                        return '<span role="tip" tip="' + col.deploy_username + ' - ' + col.deploy_time_display + '" class="am-badge am-badge-success am-round">是</span>';
                    } else {
                        return '<span class="am-badge am-badge-warning am-round">否</span>';
                    }
                }
            }, {
                display: "是否验证",
                sort: "is_verification",
                width: "6%",
                view: function(col) {
                    if (col.is_verification === 1) {
                        return '<span class="am-badge am-badge-success am-round">是</span>';
                    } else {
                        return '<span class="am-badge am-badge-warning am-round" style="background-color:#bd1cda;">否</span>';
                    }
                }
            }, {
                display: "操作",
                width: "8%",
                view: function(col) {
                    var view = '';

                    view += '<button type="button" data-id="' + col.id + '" data-package-project-id="' + col.package_project_id + '" data-action="info" class="am-btn am-btn-secondary am-btn-mini am-radius">查看升级包</button>';
                    return view;
                }
            }
        ],
        loadAfterListener: function() {
            // 格式化创建时间的格式
            timeago().render(document.querySelectorAll('.need_to_be_rendered'), 'zh_CN');

            // 绑定提示
            $('[role="tip"]').mouseover(function() {
                var _this = $(this);
                _this.justToolsTip({
                    animation: "moveInTop",
                    contents: _this.attr('tip') || '',
                    gravity: 'top'
                });
            });

            // 查看升级包
            $('#contentDiv [data-action="info"]').click(function() {
                var id = $(this).attr('data-id');
                var packageProjectId = $(this).attr('data-package-project-id');
                var url = "/simpleDownUpgradePackage/info?id=" + id + "&packageProjectId=" + packageProjectId + "&_=" + new Date().getTime();
                zeroModal.show({ url: url, title: '查看升级包', width: '70%', height: '70%', onClosed: function() { workGrid.load(); } });
            });

            if (firstQuery) {
                setTimeout(function() {
                    $('[role-id="is_finish_deploy"][role-val="0"]').click();
                    firstQuery = false;    
                }, 300);
            }
        }
    });
    workGrid.render();

    // 查询
    function load(params) {
        workGrid.load(1, params);
    }

    function noHighlight(str) {
        if (str === undefined || str === null || str === '') {
            return str;
        }

        var re = /#\d*\s/g;
        var allStr;

        var keywords = [];
        while ((allStr = re.exec(str)) !== null) {
            keywords.push(allStr[0]);
        }
        for (var i = 0; i < keywords.length; i++) {
            str = str.replace(keywords[i], '<font color="#EF971C">' + keywords[i] + '</font>');
        }
        return str;
    }
});
</script>